package com.wsd.tank;

import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;

/**
 * 	子弹类
 * @author fengmoboy
 *
 */
public class Bullet {
	private static final int SPEED = PorpertyMgr.build().get("bulletSpeed");;
	
	private Rectangle rectBullet = new Rectangle();
	
	public int width = 20;
	
	public int height = 20;
	
	private int x;
	
	private int y;
	
	private Direction dir;
	
	private boolean living = true;
	
	private TankFrame tf;
	
	private Tank tank;
	
	private Group group = Group.BAD;
	
	private BufferedImage bulletImage = null;
	
	public Bullet(int x, int y, Direction dir, Group group, TankFrame tf, Tank tank) {
		int offsetX = 0;
		int offsetY = 0;
		int tankWidth = tank.getWidth();
		int tankHeight = tank.getHeight();
		// 根据方向判断获取那张图片及子弹位置的偏移量信息
		switch (dir) {
			case LEFT:
				bulletImage = ResourceMgr.bulletL;
				offsetY = (int)((tankHeight-bulletImage.getHeight())/2);
				break;
			case UP:
				bulletImage = ResourceMgr.bulletU;
				offsetX = (int)((tankWidth-bulletImage.getWidth())/2);
				break;
			case RIGHT:
				bulletImage = ResourceMgr.bulletR;
				offsetX = tankWidth;
				offsetY = (int)((tankHeight-bulletImage.getHeight())/2);
				break;
			case DOWN:
				bulletImage = ResourceMgr.bulletD;
				offsetX = (int)((tankWidth-bulletImage.getWidth())/2);
				offsetY = tankHeight;
				break;
			default:
				break;
		}
		
		this.x = x + offsetX;
		this.y = y + offsetY;
		this.width = bulletImage.getWidth();
		this.height = bulletImage.getHeight();
		this.rectBullet.setLocation(this.x, this.y);
		this.rectBullet.setSize(this.width, this.height);
		this.dir = dir;
		this.group = group;
		this.tf = tf;
		this.tank = tank;
	}
	
	public int getX() {
		return x;
	}

	public void setX(int x) {
		this.x = x;
	}

	public int getY() {
		return y;
	}

	public void setY(int y) {
		this.y = y;
	}

	public Direction getDir() {
		return dir;
	}

	public void setDir(Direction dir) {
		this.dir = dir;
	}

	public boolean isLiving() {
		return living;
	}

	public void setLiving(boolean living) {
		this.living = living;
	}

	public int getWidth() {
		return width;
	}


	public void setWidth(int width) {
		this.width = width;
	}


	public int getHeight() {
		return height;
	}


	public void setHeight(int height) {
		this.height = height;
	}

	public Group getGroup() {
		return group;
	}


	public void setGroup(Group group) {
		this.group = group;
	}


	public Rectangle getRectBullet() {
		return rectBullet;
	}

	public void setRectBullet(Rectangle rectBullet) {
		this.rectBullet = rectBullet;
	}

	// 绘制子弹
	public void paint(Graphics g) {
		if(!living) {
			tf.bullets.remove(this);
		}
		g.drawImage(bulletImage, x, y, null);
		move();
	}

	// 子弹移动
	private void move() {
		switch (dir) {
			case LEFT:
				x -= SPEED;
				break;
			case UP:
				y -= SPEED;
				break;
			case RIGHT:
				x += SPEED;
				break;
			case DOWN:
				y += SPEED;
				break;
			default:
				break;
		}
		this.rectBullet.setLocation(this.x, this.y);
		if(x < 0 || y < 0 || x > TankFrame.GAME_WDITH || y > TankFrame.GAME_HEIGHT) {
			// 子弹消失
			this.setLiving(false);
		}
	}

	
	// 子弹与坦克间的碰撞检测 
	public void collideWith(Tank tank) {
		if(this.group == tank.getGroup()) return;
		
		//TODO: 用一个Rectangle来记录子弹的位置
		if(rectBullet.intersects(tank.getRectTank())) {
			tank.die();
			this.die();
		}
	}


	private void die() {
		this.setLiving(false);
	}
}
